package gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;



public class Listener implements MouseWheelListener, MouseMotionListener,
MouseListener, KeyListener {
	
	//TODO: HANDLE THIS IN PIXEL-BY-PIXEL MOVEMENT
	/*if (view.playerPosition()[0] < view.getWidth()/2 &&
			view.getMapPosition()[0] <= -view.getTileSize()) {
		view.updateMapPosition(view.getTileSize(), 0);
	}*/
	
	private View view = null;
	
	private int[] lastPos;
	
	public Listener(View view) {
		this.view = view;
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		view.setTileSize(e.getWheelRotation());
		view.repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if (lastPos == null) {
			lastPos = new int[] {e.getX(), e.getY()};
		} else {
			view.updateMapPosition(e.getX()-lastPos[0], e.getY()-lastPos[1]);
			lastPos = new int[] {e.getX(),e.getY()};
			view.repaint();
		}
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		lastPos = null;
	}

	@Override
	public void mouseMoved(MouseEvent e) {}
	@Override
	public void mouseClicked(MouseEvent arg0) {}
	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			view.player().moveWest();
			break;
		case KeyEvent.VK_RIGHT:
			view.player().moveEast();
			break;
		case KeyEvent.VK_UP:
			view.player().moveNorth();
			break;
		case KeyEvent.VK_DOWN:
			view.player().moveSouth();
			break;
		}
		view.repaint();
	}

	@Override
	public void keyReleased(KeyEvent arg0) {}
	@Override
	public void keyTyped(KeyEvent arg0) {}
	
}